﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CardLogistics.Web.Components;
using CardLogistics.Model.Basic;
using System.Data;

namespace CardLogistics.Web.MainPage.Basic.SalesOutlet
{
    public partial class AddEdit : BasePage
    {
        private CardLogistics.DAL.Basic.Basic_SalesOutletData DAL = new DAL.Basic.Basic_SalesOutletData();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //SALESINFOID.Items.Insert(0, new ListItem("上级销售/机构网点", "0")); 
                BindGroup();
                BindTxt();
            }
        }

        protected void btn_Add_Click(object sender, EventArgs e)
        {
            try
            {
                string result = "";
                string logContent = "";
                if (IsEdit)
                {
                    result = DAL.Update(EncapsulationModel());
                    logContent = "修改销售机构/网点,网点名称:" + EncapsulationModel().Salesoutlet_name;
                }
                else
                {
                    result = DAL.Add(EncapsulationModel());
                    logContent = "添加销售机构/网点,网点名称:" + EncapsulationModel().Salesoutlet_name;
                }

                if (result.Split(',')[0] == "ok")
                {
                    InsertLog(logContent);
                    ReturnListInfo();
                }
                else if (result.Split(',')[1] == "-1")
                {
                    ShowMessageBox("网点名称重复,请重新输入！");
                    Salesoutlet_code.Focus();
                }
                else
                {
                    ShowMessageBox("操作失败！" + result.Split(',')[1]);
                }
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }
        /// <summary>
        /// 获取对象
        /// </summary>
        /// <returns></returns>
        private Basic_SalesOutlet EncapsulationModel()
        {
            Basic_SalesOutlet model = new Basic_SalesOutlet();
            model = GetWebControls<Basic_SalesOutlet>(this.Page);
            if (string.IsNullOrEmpty(Salesinfoid.SelectedValue))
                model.Salesinfoid = 0;
            else
                model.Salesinfoid = int.Parse(Salesinfoid.SelectedValue);
            model.Status = Status.Checked? 1:0;
            if (Startdate.Text.Trim().Length > 0)
                model.Startdate = DateTime.Parse(Startdate.Text.Trim());
            if (Enddate.Text.Trim().Length > 0)
                model.Enddate = DateTime.Parse(Enddate.Text.Trim());
            if (IsEdit)
                model.Id = int.Parse(Request.QueryString["id"]);
            return model;
        }

        /// <summary>
        /// 绑定值
        /// </summary>
        private void BindTxt()
        {
            string id = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(id))
            {
                Basic_SalesOutlet Data = DAL.GetModel(id);
                SetWebControls(this.Page, Data);
                if (Data.Startdate != null)
                {
                    Startdate.Text = Data.Startdate.Value.ToString("yyyy-MM-dd");
                }
                if (Data.Enddate != null)
                {
                    Enddate.Text = Data.Enddate.Value.ToString("yyyy-MM-dd");
                }
                Salesoutlet_remak.Value = Data.Salesoutlet_remak;
                Status.Checked = Data.Status==0? false:true;
                Salesinfoid.SelectedValue = Data.Salesinfoid.ToString();
            }
        }

        #region 绑定机构/网点树列表
        /// <summary>
        /// 绑定根节点
        /// </summary>
        void BindGroup()
        {
            DataTable dt = new CardLogistics.DAL.Basic.Basic_SalesInfoData().GetList(" ISDELETE=0 ", "*");//获取所有节点
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    if (row["PARENTID"].ToString().Trim() == "0")//绑定根节点
                    {
                        this.Salesinfoid.Items.Insert(Salesinfoid.Items.Count, new ListItem(row["SALESNAME"].ToString(), row["Id"].ToString()));
                        this.BindDropChildItem(this.Salesinfoid, dt, row["Id"].ToString(), 1);
                    }
                }
            }
        }
        /// <summary>
        /// 绑定子节点
        /// </summary>
        /// <param name="d"></param>
        /// <param name="dt"></param>
        /// <param name="id"></param>
        /// <param name="length"></param>
        void BindDropChildItem(DropDownList d, DataTable dt, string id, int length)
        {
            DataRow[] rows = dt.Select("PARENTID='" + id + "'", "Id DESC");//取出id子节点进行绑定
            for (int i = 0; i < rows.Length; i++)
            {
                this.Salesinfoid.Items.Insert(Salesinfoid.Items.Count, new ListItem(this.SpaceLength(length) + rows[i]["SALESNAME"].ToString(), rows[i]["Id"].ToString()));
                this.BindDropChildItem(d, dt, rows[i]["Id"].ToString(), length + 1);//空白数目加1
            }
        }
        /// <summary>
        /// 子节点前面的空白数
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        string SpaceLength(int i)
        {
            string space = "┊";
            for (int j = 0; j < i; j++)
            {
                space += "┈┈";//注意这里的空白是智能abc输入法状态下的v11字符；┊┈┈
            }
            return space;
        }
        #endregion

    }
}